<?php
/**
 * 专家接口
 * 获取专家信息,专家认证
 * 验证用户身份token
 * @author Edwin
 */

namespace V1\Controller;

use Common\Controller\ApiController;
use Think\Exception;

class ExpertController extends ApiController
{
    public function rest()
    {
        switch ($this->_method) {
            case 'get': // get请求处理代码
                switch (I('get.type')) {
                    case 'invitation':
                        $this->_getExpertList();
                        break;
                    default:
                        $this->_get();
                        break;
                }
                break;
            case 'post': // post请求处理代码
                switch (I('post.type')) {
                    case 'invitation':
                        $this->_invitationExpert();
                        break;
                    default:
                        $this->_post();
                        break;
                }
                break;
            case 'put': // put更新操作
                $this->_put();
                break;
        }
    }

    /**
     * 获取输出专家信息
     */
    private function _get()
    {
        $userid = $this->userId;
        try {
            $request = D('ExpertProfile')->get_expert_profile($userid);
            if (!$request) {
                $result['msg'] = C('STATUS')['245'];
                $result['code'] = 201;
                $result['result'] = [];
            } else {
                if($request['status']){
                    $result['msg'] = C('STATUS')['230'];
                    $result['code'] = 200;
                    $result['result'] = $request;
                }else{
                    $result['msg'] = C('STATUS')['245'];
                    $result['code'] = 202;
                    $result['result'] = [];
                }
            }
        } catch (Exception $exception) {
            $result['msg'] = C('STATUS')['230'];
            $result['code'] = 200;
            $result['result'] = [];
        }
        $this->myApiPrint($result['msg'], $result['code'], $result['result']);
    }

    /**
     * 上传专家认证资料
     */
    private function _post()
    {
        $data['userid'] = $this->userId;
        $data['name'] = I('post.name');
        $expert_type = D('ExpertLevelProfile')->get_expert_level(I('post.expert_type'));
        $data['expert_level'] = !empty(I('post.expert_type')) ? I('post.expert_type') : '1';
        $data['expert_type'] = $expert_type['level_title'];
        $data['good_at_crop'] = !empty(I('post.good_at_crop')) ? I('post.good_at_crop') : '';
        $data['good_at_area'] = !empty(I('post.good_at_area')) ? I('post.good_at_area') : '';
        $data['qq'] = !empty(I('post.qq')) ? I('post.qq') : '';
        $data['postion'] = !empty(I('post.postion')) ? I('post.postion') : '';
        $data['company'] = !empty(I('post.company')) ? I('post.company') : '';
        $data['id_card_front'] = !empty(I('post.id_card_front')) ? I('post.id_card_front') : '';
        $data['id_card_back'] = !empty(I('post.id_card_back')) ? I('post.id_card_back') : '';
        $data['title_card_front'] = !empty(I('post.title_card_front')) ? I('post.title_card_front') : '';
        $data['title_card_back'] = !empty(I('post.title_card_back')) ? I('post.title_card_back') : '';
        $data['content'] = !empty(I('post.content')) ? I('post.content') : '';
        $data['addtime'] = time();
        $info = D('ExpertProfile')->add($data);
        if ($info) {
            $this->myApiPrint('提交成功！', 200);
        } else {
            $this->myApiPrint('提交失败，请稍后再试', 201);
        }
    }

    /**
     * 更新专家认证资料
     */
    private function _put()
    {
        $data['userid'] = $this->userId;
        $data['name'] = I('put.name');
        $expert_type = D('ExpertLevelProfile')->get_expert_level(I('put.expert_type'));
        $data['expert_level'] = !empty(I('put.expert_type')) ? I('put.expert_type') : '1';
        $data['expert_type'] = $expert_type['level_title'];
        $data['good_at_crop'] = I('put.good_at_crop');
        $data['good_at_area'] = I('put.good_at_area');
        $data['qq'] = !empty(I('put.qq')) ? I('put.qq') : '';
        $data['postion'] = !empty(I('put.postion')) ? I('put.postion') : '';
        $data['company'] = !empty(I('put.company')) ? I('put.company') : '';
        $data['id_card_front'] = I('put.id_card_front');
        $data['id_card_back'] = I('put.id_card_back');
        $data['title_card_front'] = I('put.title_card_front');
        $data['title_card_back'] = I('put.title_card_back');
        $data['content'] = !empty(I('put.content')) ? I('put.content') : '';
        $data['updatetime'] = time();
        $info = D('ExpertProfile')->save($data);
        if ($info) {
            $this->myApiPrint('更新成功！', 200);
        } else {
            $this->myApiPrint('更新失败，请稍后再试', 201);
        }
    }

    /**
     * 上传证件图片
     */
    public function addImage()
    {
        $stu_id['stu_id'] = $this->userId; //获取到用户的ID
        $data = base64imgsave(I('post.img'), $this->userId, 'front');
        $this->ajaxReturn($data);
    }

    /**
     * 获取专家列表
     */
    public function _getExpertList()
    {
        $info = I('get.');
        $info['invitation_uid'] = $this->userId;
        $request = D('ExpertProfile')->getInviteExpert($info);
        if (!$request) {
            $this->myApiPrint(C('STATUS')['230'], 201, $request);
        } else {
            $this->myApiPrint(C('STATUS')['200'], 200, $request);
        }

    }

    /**
     * 获取邀请过的专家列表
     */
    public function getInviteExpertList()
    {
        $uid = !empty(I('get.uid')) ? trim(I('get.uid')) : $this->userId;
        $start = trim(I('get.start'));
        $limit = trim(I('get.limit'));
        $my_invite_expert_list['list'] = D('Message')->get_my_invite_expert_list($uid, $start, $limit);
        $my_invite_expert_list['count'] = count($my_invite_expert_list['list']);
        if ($my_invite_expert_list) {
            $this->myApiPrint(C('STATUS')['200'], 200, $my_invite_expert_list);
        } else {
            $this->myApiPrint(C('STATUS')['230'], 201);
        }
    }

    /**
     * 邀请专家
     */
    public function _invitationExpert()
    {
        $info = I('post.');
        $info['invitation_uid'] = $this->userId;
        $request = D('Message')->addInviteExpert($info);
        $this->myApiPrint($request['msg'], $request['code'], $request['result']);
    }
}
